package org.theqcp.wow.sim.gui;

import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;
import javax.swing.KeyStroke;

public class ESGMenuBar extends JMenuBar {

    private static final long serialVersionUID = -769152576462367967L;

    protected final KeyStroke keyCtrlL = KeyStroke.getKeyStroke(KeyEvent.VK_L, KeyEvent.CTRL_DOWN_MASK);
    protected final KeyStroke keyCtrlN = KeyStroke.getKeyStroke(KeyEvent.VK_N, KeyEvent.CTRL_DOWN_MASK);
    protected final KeyStroke keyCtrlO = KeyStroke.getKeyStroke(KeyEvent.VK_O, KeyEvent.CTRL_DOWN_MASK);
    protected final KeyStroke keyCtrlS = KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_DOWN_MASK);
    protected final KeyStroke keyCtrlShiftS = KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_DOWN_MASK |
	KeyEvent.SHIFT_DOWN_MASK);
    protected final KeyStroke keyCtrlX = KeyStroke.getKeyStroke(KeyEvent.VK_X, KeyEvent.CTRL_DOWN_MASK);
    protected final KeyStroke keyF1 = KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0);
    protected final KeyStroke keyF2 = KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0);
    protected final KeyStroke keyF3 = KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0);
    protected final KeyStroke keyF5 = KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0);
    protected final KeyStroke keyF6 = KeyStroke.getKeyStroke(KeyEvent.VK_F6, 0);
    protected final KeyStroke keyF7 = KeyStroke.getKeyStroke(KeyEvent.VK_F7, 0);
    protected final KeyStroke keyF8 = KeyStroke.getKeyStroke(KeyEvent.VK_F8, 0);

    public ESGMenuBar(ActionListener listener) {
	JMenu mFile = newMenu("File", 'f');
	JMenu mImport = newMenu("Import", 'i');
	JMenu mSimulate = newMenu("Simulate", 'm');
	JMenu mHelp = newMenu("Help", 'p');

	add(mFile);
	add(mImport);
	add(mSimulate);
	add(Box.createHorizontalGlue());
	add(mHelp);

	mFile.add(newMenuItem("New", "new_char", listener, 'n', keyCtrlN));
	mFile.add(new JSeparator());
	mFile.add(newMenuItem("Load...", "load_char_file", listener, 'l', keyCtrlL));
	mFile.add(newMenuItem("Save...", "save_char_file", listener, 's', keyCtrlS));
	mFile.add(newMenuItem("Save as...", "save_as_char_file", listener, 'a', keyCtrlShiftS));
	mFile.add(new JSeparator());
	mFile.add(newMenuItem("Exit", "exit", listener, 'x', keyCtrlX));

	mImport.add(newMenuItem("Character from WoW Armory...", "import_char", listener, 'c', keyF5));
	mImport.add(new JSeparator());
	mImport.add(newMenuItem("Item from WoW Armory...", "import_item_armory", listener, 'r', keyF6));
	mImport.add(newMenuItem("Item from Wowhead...", "import_item_head", listener, 'h', keyF7));
	mImport.add(newMenuItem("Item from WoWWiki...", "import_item_wiki", listener, 'w', keyF8));

	mSimulate.add(newMenuItem("Start combat sim", "enh_sim_start", listener, 't', keyF2));
	mSimulate.add(newMenuItem("Calculate EP values", "enh_sim_calc_ep", listener, 'e', keyF3));

	mHelp.add(newMenuItem("About", "about", listener, 'a', keyF1));
    }

    private JMenu newMenu( String label, char mnemonic ) {
	JMenu menu = new JMenu(label);
	menu.setLayout(new BoxLayout(menu, BoxLayout.X_AXIS));

	if( mnemonic != 0 ) {
	    menu.setMnemonic(mnemonic);
	}

	return menu;
    }

    private JMenuItem newMenuItem( String label, String actionCommand, ActionListener listener, char mnemonic,
	    KeyStroke accelerator ) {
	JMenuItem item = new JMenuItem(label);
	item.setActionCommand(actionCommand);
	item.addActionListener(listener);

	if( mnemonic != 0 ) {
	    item.setMnemonic(mnemonic);
	}

	if( accelerator != null ) {
	    item.setAccelerator(accelerator);
	}

	return item;
    }

}
